// Common/MemoryLock.cpp

#include "StdAfx.h"

namespace Common {

namespace System {
namespace Security {

#ifndef UNDER_CE


BOOL EnableLockMemoryPrivilege(BOOL enable)
{
  HANDLE token;
  if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
    return false;
  TOKEN_PRIVILEGES tp;
  BOOL res = FALSE;
  if (::LookupPrivilegeValue (NULL, SE_LOCK_MEMORY_NAME, &(tp.Privileges[0].Luid)))
  {
    tp.PrivilegeCount = 1;
    tp.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED: 0;
    if (::AdjustTokenPrivileges(token, FALSE, &tp, 0, NULL, NULL))
      res = (GetLastError() == ERROR_SUCCESS);
  }
  ::CloseHandle(token);
  return res;
}

#endif

}}
}